$().ready(function() {
	initDictDatas();
	validateRule();
	//提点转换为整数
	var deduct = $('#deduct').val();
	if(deduct == null || deduct == ''){
		$('#deduct').val(0);
	}else{
		var p = parseFloat(deduct)*100;
		$('#deduct').val(p.toFixed(0));
	}
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
//构建字典数据
function initDictDatas(){
	
	$('#cpTypeId').bootstrapSelect({
	    url:ctx+'plat/cpType/listCpTypeDict',
	    valueField: 'typeId',
	    textField: 'typeName',
	    placeholder: "请选择优惠券类型",
	    onChange:function(value){
	    }
	});
	
	$('#cpTypeId').bootstrapSelect('setValue',cpTypeId);

}
function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : ctx+"plat/shopCprule/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			cpTypeId : {
				required : true,
			},
			cpNum : {
				required : true,
				isPositiveInt:true,
				max: 99999,
				min: 1
			},
			deduct : {
				required : true,
				isPositiveInt:true,
				max:99,
				min:0
			}
		},
		messages : {
			cpTypeId : {
				required : icon + "请选择券类型"
			},
			cpNum : {
				required : icon + "请输入可发券数量",
				max: icon + "请输入小于9999的正整数",
				min: icon + "请输入大于0的正整数"
			},
			deduct : {
				required : icon + "请输入提点数",
				max: icon + "请输入大于等于0并且小于100的正整数",
				min: icon + "请输入大于等于0并且小于100的正整数"
			}
		},
		errorPlacement: function(error, element) {
			if (element[0].tagName && element[0].tagName.toLowerCase() == 'select'){
				error.appendTo(element.parent().parent());
			}else {					
				error.appendTo(element.parent());
			}
		}
	})
}